  
package suncertify.Server;

import suncertify.shared.ServerProperty;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jshah
 * A runnable class which creates a Remote ServerBrokerImplementation object and binds it to the RMI URL
 */
public class ServerThread implements Runnable{

    /**
     * String remoteUrl  URL of the RMI Server
     */
    String remoteUrl;

    /**
     * constructor which takes a String "rmiUrl" as an argument for the url of the rmi server
     * @param rmiUrl
     */
    public ServerThread(String rmiUrl){
        
   //     ServerProperty.setDefaultProp();
        this.remoteUrl = rmiUrl;
      
    }

    public void run(){

       Registry r;
    //   String remoteUrl = "//" + ServerProperty.getServerRmiHostProp()+"/"+ServerProperty.getServerRmiUrlProp();
       Logger.getLogger(ServerThread.class.getName()).log(Level.INFO, this.remoteUrl);
       ServerBrokerImplementation serverInstance = null;
        try {
            serverInstance = new ServerBrokerImplementation();
        } catch (RemoteException ex) {
            Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
        }
     
        try { //special exception handler for registry creation
            int port = Integer.parseInt(ServerProperty.getServerRmiPortProp());
            LocateRegistry.createRegistry(port);
            System.out.println("java RMI registry created.");
        }catch(NumberFormatException ne){
            Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ne);
        }
        catch (RemoteException e) {
            //do nothing, error means registry already exists
            System.out.println("java RMI registry already exists.");
        }
        try {
            Naming.rebind(this.remoteUrl, serverInstance);
            System.out.append("REMOTE OBJECT BOUND, SERVER IS RUNNING");
        } catch (RemoteException ex) {
            Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(ServerThread.class.getName()).log(Level.SEVERE, null, ex);
        }



       while(true){
           
       }

    }
}
